﻿var J_tabmove = $("#J_tabmove");
//element cache
var J_mobile = $("#J_mobile");
var J_creditcard = $("#J_creditcard");
var J_movie_city = $("#J_movie_city");
var J_movie_cinema = $("#J_movie_cinema");
var J_movie_film = $("#J_movie_film");
var J_recent_buy = $("#J_recent_buy");
var J_menu = $("#J_menu");
var J_slide_lottery = $('#J_slide_lottery')

$(document).ready(function() {
    tab($("#J_handy"));
    //公告滚动
    var notice = $("#J_notice");
    var notice_html = notice.html();
    if (notice_html.length > 36) {
        notice.html("<marquee direction='left' scrollamount='2' scrolldelay='1' onmouseover='this.stop()' onmouseover='this.stop()' onmouseout='this.start()'>" + notice_html + "</marquee>");
    };
    J_tabmove.find(".quick-tabup").attr("href", "javascript:void(0)").click(function() {
        var steps = 83;
        var left = parseInt(J_tabmove.find("table").css("left"));
        if (isNaN(left)) { left = 0; };
        if (left < 0) {
            left += steps;
        };
        J_tabmove.find("table").animate({ left: left }, 800);
    });
    J_tabmove.find(".quick-tabdown").attr("href", "javascript:void(0)").click(function() {
        var step = J_tabmove.find("table").find("td").length - 6;
        var steps = 83;
        var left = parseInt(J_tabmove.find("table").css("left"));
        if (isNaN(left)) { left = 0; };
        if (left > (-step * steps)) {
            left += -steps;
        };
        J_tabmove.find("table").animate({ left: left }, 800);
    });

    //便民
    //手机冲值
    J_mobile.submit(function () {
        var t = $(this);
        var input = t.find("input");
        var reg = /^(((13[0-9]{1})|(147)|(15[0-35-9]{1})|(18[025-9]{1}))+\d{8})$/;
        var i0 = input.eq(0).val().replace(/(^\s*)|(\s*$)/g, "");
        var i1 = input.eq(1).val().replace(/(^\s*)|(\s*$)/g, "");
        if (!reg.exec(i0)) { alert("请填写正确的手机号码"); return false; };
        if (i1 != i0) { alert("请确认两次输入手机号码是否相同"); return false; };
    });
    J_mobile.find("input[type='text']").keyup(function () {
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            val = val.substring(0, 11);
            t.val(val);
        };
    });
    //电影票
    J_movie_cinema.parent().next(".handy-btn").find("a").attr("href", "javascript:alert('请选择城市');");
    var movie_city_html = "";
    for (var i = 0; i < _handy_movies.length; i++) {
        movie_city_html += _handy_movies[i].parentId == 0 ? "<p data='" + _handy_movies[i].id + "'>" + _handy_movies[i].name + "</p>" : "";
    };
    J_movie_city.find(".handy-selectcontent").html(movie_city_html);
    J_movie_city.find("input").val("请选择城市");
    J_movie_film.find("input").val("请选择电影");
    J_movie_cinema.find("input").val("请选择影院");
    J_movie_city.click(function () {
        var t = $(this).find("input");
        J_movie_city.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_movie_city.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
        J_movie_film.find(".handy-selectcontent").hide();
        J_movie_cinema.find(".handy-selectcontent").hide();
    });
    J_movie_city.find(".handy-selectcontent p").live("click", function () {
        J_movie_city.find("input").val($(this).html()).attr("data", $(this).attr("data"));
        J_movie_city.find(".handy-selectcontent").hide();
        J_movie_cinema.find(".handy-selectcontent").html("");
        //刷新影片
        var url = J_movie_film.find("input").attr("data-load") + "?r=" + Math.random() + "&city=" + $(this).attr("data");
        J_movie_film.find("input").val("请选择电影");
        J_movie_cinema.find("input").val("请选择影院");
        $.getJSON(url, function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += "<p data='" + data[i].id + "'>" + data[i].name + "</p>";
            };
            J_movie_film.find(".handy-selectcontent").html(html);
        });
        J_movie_cinema.parent().next(".handy-btn").find("a").attr("href", "javascript:alert('请选择电影');").removeAttr("target");
    });
    J_movie_film.click(function () {
        var t = $(this).find("input");
        J_movie_film.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_movie_film.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
        J_movie_cinema.find(".handy-selectcontent").hide();
    });
    J_movie_film.find(".handy-selectcontent p").live("click", function () {
        J_movie_film.find("input").val($(this).html()).attr("data", $(this).attr("data"));
        J_movie_film.find(".handy-selectcontent").hide();
        //刷新影院
        var url = J_movie_cinema.find("input").attr("data-load") + "?r=" + Math.random() + "&city=" + J_movie_city.find("input").attr("data") + "&film=" + $(this).attr("data");
        J_movie_cinema.find("input").val("请选择影院");
        $.getJSON(url, function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += "<p data='" + data[i].id + "'>" + data[i].name + "</p>";
            };
            J_movie_cinema.find(".handy-selectcontent").html(html);
        });
        //超链接
        var href = J_movie_cinema.parent().next(".handy-btn").find("a");
        var url = href.attr("data-href");
        url = url.replace("{city}", J_movie_city.find("input").attr("data"));
        url = url.replace("{film}", J_movie_film.find("input").attr("data"));
        url = url.replace("&cinema={cinema}", "");
        href.attr("href", url).attr("target", "_blank");
    });
    J_movie_cinema.click(function () {
        var t = $(this).find("input");
        J_movie_cinema.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_movie_cinema.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
    });
    J_movie_cinema.find(".handy-selectcontent p").live("click", function () {
        J_movie_cinema.find("input").val($(this).html()).attr("data", $(this).attr("data"));
        J_movie_cinema.find(".handy-selectcontent").hide();
        //超链接
        var href = J_movie_cinema.parent().next(".handy-btn").find("a");
        var url = href.attr("data-href");
        url = url.replace("{city}", J_movie_city.find("input").attr("data"));
        url = url.replace("{film}", J_movie_film.find("input").attr("data"));
        url = url.replace("{cinema}", J_movie_cinema.find("input").attr("data"));
        href.attr("href", url).attr("target", "_blank");
    });

    //信用卡
    var cardhtml = "";
    for (var i = 0; i < _handy_creditcards.length; i++) {
        cardhtml += "<p data='" + _handy_creditcards[i].bankRedirectURL + "'>" + _handy_creditcards[i].bankName + "</p>";
    };
    J_creditcard.find(".handy-selectcontent").html(cardhtml);
    J_creditcard.click(function () {
        var t = $(this).find("input");
        J_creditcard.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_creditcard.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
    });
    J_creditcard.find(".handy-selectcontent p").live("click", function () {
        J_creditcard.find("input").val($(this).html());
        J_creditcard.find(".handy-selectcontent").hide();
        J_creditcard.parent().next(".handy-btn").find(".handy-abtn").attr("href", $(this).attr("data")).attr("target", "_blank");
    });
    J_creditcard.parent().next(".handy-btn").find(".handy-abtn").attr("href", "javascript:void(0)");

    //个人信息
    var parm = { "random": Math.random() };
    $("#J_auth_mobile").load($("#J_auth_mobile_url").val(),parm);
    $("#J_auth_email").load($("#J_auth_email_url").val(), parm);
    $("#J_auth_id").load($("#J_auth_id_url").val(), parm);
    $.getJSON($("#J_dash_money_url").val(), parm, function(data) {
        $("#J_dash_money").html(data.availableBalance);
    });
    $("#J_dash_coupon").load($("#J_dash_coupon_url").val(), parm);
    $("#J_dash_score").load($("#J_dash_score_url").val(), parm);

    //抽奖
    //初始化
    J_slide_lottery.find("li").html("<a href='" + J_slide_lottery.attr("data-url") + "' target='_blank'></a>");
    J_slide_lottery.find('.slide').eq(0).attr("data-eq", 0).find(".slide-list").css({ "top": (-88 * 0) + "px" });
    J_slide_lottery.find('.slide').eq(1).attr("data-eq", 1).find(".slide-list").css({ "top": (-88 * 1) + "px" });
    J_slide_lottery.find('.slide').eq(2).attr("data-eq", 2).find(".slide-list").css({ "top": (-88 * 2) + "px" });
    J_slide_lottery.find('.slide').eq(3).attr("data-eq", 3).find(".slide-list").css({ "top": (-88 * 3) + "px" });
    setTimeout("lottery_play()", 1000);
    setInterval("lottery_play()", 8000);
});

//tab
function tab(element) {
    element.find(".tab-holder").children().mouseover(function() {
        $(this).parent().find(".tab-current").removeClass("tab-current");
        $(this).addClass("tab-current");
        var eq = $.inArray(this, $.makeArray(element.find(".tab-holder").children()));
        element.find(".tab-panel").children().hide().eq(eq).show();
    }).eq(0).mouseover();
    var href = element.find(".tab-holder a");
    if (href.attr("href") == "" || href.attr("href") == "#" || href.attr("href") == null) { href.attr("href", "javascript:void(0)"); }
};

//轮播
function slide(element, step, time) {
    var slide_list = element.find(".slide-list");
    var slide_triggers = element.find(".slide-triggers");
    var count = slide_list.children().length;
    if ((count < 2) || (slide_list.height() < element.height() + step)) { return; }
    var slide_list_first = slide_list.children().eq(0);
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = -1; element.attr("data-eq", "0"); };
    eq += 1;
    if (eq > count - 1) {
        //slide_list_first.css({ "position": "relative", "top": (count * step) + "px" });
        eq = 0;
        slide_list.animate({ top: -count * step }, time, function() {
            slide_list.animate({ top: 0 }, 0);
            slide_list.children().removeAttr("style");
        });
    } else {
        slide_list.animate({ top: -eq * step }, time);
    }
    if (slide_list.height() - count * step < element.height()) {
        for (var i = 0; i < eq - 1; i++) {
            slide_list.children().eq(i).css({ "position": "relative", "top": (count * step) + "px" });
        };
    };
    element.attr("data-eq", eq);
    slide_triggers.children().removeClass("slide-current").eq(eq).addClass("slide-current");
};

//抽奖
function lottery_play() {
    s1 = Math.random() * 200; s1 += s1 < 65 ? 26 : 0;
    s2 = Math.random() * 200; s2 += s2 < 52 ? 39 : 0;
    s3 = Math.random() * 200; s3 += s3 < 39 ? 52 : 0;
    s4 = Math.random() * 200; s4 += s4 < 26 ? 65 : 0;
    lt1 = setInterval("slide(J_slide_lottery.find('.slide').eq(0),68," + s1 + ")", s1);
    lt2 = setInterval("slide(J_slide_lottery.find('.slide').eq(1),68," + s2 + ")", s2);
    lt3 = setInterval("slide(J_slide_lottery.find('.slide').eq(2),68," + s3 + ")", s3);
    lt4 = setInterval("slide(J_slide_lottery.find('.slide').eq(3),68," + s4 + ")", s4);
    setTimeout("clearInterval(lt1); clearInterval(lt2); clearInterval(lt3); clearInterval(lt4);", 5000);
    setTimeout("J_slide_lottery.find('li').removeAttr('style');$('#J_slide_lottery').find('.slide').eq(0).attr('data-eq', 0).find('.slide-list').css({ 'top': (-88 * 0) + 'px' });$('#J_slide_lottery').find('.slide').eq(1).attr('data-eq', 1).find('.slide-list').css({ 'top': (-88 * 1) + 'px' });$('#J_slide_lottery').find('.slide').eq(2).attr('data-eq', 2).find('.slide-list').css({ 'top': (-88 * 2) + 'px' });$('#J_slide_lottery').find('.slide').eq(3).attr('data-eq', 3).find('.slide-list').css({ 'top': (-88 * 3) + 'px' });", 5200);
    setTimeout("$('#J_slide_lottery').find('li').removeAttr('style');$('#J_slide_lottery').find('.slide').eq(0).attr('data-eq', 0).find('.slide-list').css({ 'top': (-88 * 0) + 'px' });$('#J_slide_lottery').find('.slide').eq(1).attr('data-eq', 1).find('.slide-list').css({ 'top': (-88 * 1) + 'px' });$('#J_slide_lottery').find('.slide').eq(2).attr('data-eq', 2).find('.slide-list').css({ 'top': (-88 * 2) + 'px' });$('#J_slide_lottery').find('.slide').eq(3).attr('data-eq', 3).find('.slide-list').css({ 'top': (-88 * 3) + 'px' });", 5500);
    setTimeout("$('#J_slide_lottery').find('li').removeAttr('style');$('#J_slide_lottery').find('.slide').eq(0).attr('data-eq', 0).find('.slide-list').css({ 'top': (-88 * 0) + 'px' });$('#J_slide_lottery').find('.slide').eq(1).attr('data-eq', 1).find('.slide-list').css({ 'top': (-88 * 1) + 'px' });$('#J_slide_lottery').find('.slide').eq(2).attr('data-eq', 2).find('.slide-list').css({ 'top': (-88 * 2) + 'px' });$('#J_slide_lottery').find('.slide').eq(3).attr('data-eq', 3).find('.slide-list').css({ 'top': (-88 * 3) + 'px' });", 6500);
};